//
//  HMStatusCell.swift
//  webo
//
//  Created by apple on 2016/11/16.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SDWebImage
private let margin: CGFloat = 8
private let imageMargin: CGFloat = 2
private let maxWidth:CGFloat = ScreenWidth - 2 * margin
private let itemWidth:CGFloat = (maxWidth - 2 * imageMargin) / 3


class HMStatusCell: UITableViewCell {
    @IBOutlet weak var iconView: UIImageView!
    @IBOutlet weak var AvatarView: UIImageView!
    @IBOutlet weak var NameLabel: UILabel!
    @IBOutlet weak var MbRankView: UIImageView!
    @IBOutlet weak var TimeLabel: UILabel!
    @IBOutlet weak var SourceLabel: UILabel!
    @IBOutlet weak var ContentLabel: YYLabel!
    
    @IBOutlet weak var PictureView: HMPictureView!
    @IBOutlet weak var pictureViewHeight: NSLayoutConstraint!
    @IBOutlet weak var pictureflowLatout: UICollectionViewFlowLayout!
    @IBOutlet weak var pictureViewWeight: NSLayoutConstraint!
    
    @IBOutlet weak var pictureTopContaint: NSLayoutConstraint!
    @IBOutlet weak var uv_topView: UIView!
    @IBOutlet weak var lb_transmit: YYLabel!
    @IBOutlet weak var uv_bottomTool: UIView!
    var viewModel: HMStatusViewModel? {
        didSet{
        
            iconView.sd_setImage(with: viewModel?.iconURL)
            lb_transmit?.attributedText = viewModel?.retWeetedAttributedString
            
            NameLabel.text = viewModel?.status?.user?.name
            TimeLabel.text = viewModel?.timeText
//            SourceLabel.text = viewModel?.status?.source
            SourceLabel.text = viewModel?.sourceText
//            ContentLabel.text = viewModel?.status?.text
            ContentLabel.attributedText = viewModel?.originalAttributedString
            AvatarView.image = viewModel?.verifitedImage
            MbRankView.image = viewModel?.mbRankImage
            
            let count = viewModel?.imgUrls?.count ?? 0
            
            pictureTopContaint?.constant = (count == 0 ? 0 : 8)

            let pSize = caculatePictureViewSize(count: count)
            pictureViewHeight.constant = pSize.psize.height
            pictureViewWeight.constant = pSize.psize.width
            pictureflowLatout.itemSize = pSize.itemSize
//        print(pSize)
//        lb_transmit?.text = viewModel?.status?.retweeted_status?.text
            
         
            PictureView.pictureInfos = viewModel?.imgUrls
            PictureView.layoutIfNeeded()
//            typedef void(^YYTextAction)(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect);
            ContentLabel.highlightTapAction =  { (containerView,text,range,rect) -> () in
                //containerView,就是被点击的视图
                //text: 被点击的属性文本
                //range: 点击的特殊文本 范围
                //rect: 点击的特殊的文本的坐标
                let sub = (text.string as NSString).substring(with: range)
                print(sub)
                //跳转页面   响应者链条来实现页面跳转
                if sub.hasPrefix("http") {
                    //实例化微博
                    let web = HMWebViewController()
                    web.urlString = sub
                   self.findNavigationController()?.pushViewController(web, animated: true)
                    
                }
            }
            
            lb_transmit?.highlightTapAction =  { (containerView,text,range,rect) -> () in
    
                let sub = (text.string as NSString).substring(with: range)
                print(sub)
                if sub.hasPrefix("http") {
                    //实例化微博
                    let web = HMWebViewController()
                    web.urlString = sub
                    self.findNavigationController()?.pushViewController(web, animated: true)
                }
            }
        }
    }
    
    
    func caculatePictureViewSize(count: Int) -> (psize:CGSize,itemSize:CGSize){
        if count == 0 {
            
            return (CGSize(width: 1, height: 1),CGSize(width: 1, height: 1))
        }
        
        if count == 4 {
            let width = 2 * itemWidth + imageMargin
            return (CGSize(width: width, height: width), CGSize(width: itemWidth, height: itemWidth))
        }
        
        if count == 1 {
            if let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: viewModel?.imgUrls?.first?.wap360 ?? "") {
               
                let imageSize = image.dealScaleImg(width: maxWidth).size
                return (imageSize,imageSize)
            }
         
            return (CGSize(width: itemWidth, height: itemWidth),CGSize(width: itemWidth, height: itemWidth))

        }
        /*
         1,2,3  1
         4,5,6  2
         7,8,9  3
         */
        let row = CGFloat((count-1) / 3 ) + 1
        let h = row * itemWidth + imageMargin * (row - 1)
        return (CGSize(width: maxWidth , height: h),CGSize(width: itemWidth , height: itemWidth))
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
//        选中的类型
        self.selectionStyle = .none
        
        self.lb_transmit?.numberOfLines = 0
        self.lb_transmit?.preferredMaxLayoutWidth = ScreenWidth - margin * 2
        
        self.ContentLabel.numberOfLines = 0
        self.ContentLabel.preferredMaxLayoutWidth = ScreenWidth - margin * 2
        
        pictureflowLatout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        pictureflowLatout.minimumLineSpacing = imageMargin
        pictureflowLatout.minimumInteritemSpacing = imageMargin
        
    
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
